package game.poker.holdem.card;

import game.poker.holdem.HoldEmUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class Deck {
    private final int SIZE;
	private List<Card> cards;

	public Deck() {
	    Suit[] suits = Suit.values();
	    Rank[] ranks = Rank.values();
	    
	    SIZE = suits.length * ranks.length;
		this.cards = new ArrayList<Card>(SIZE);
		
		for(Suit suit : suits) {
		    for(Rank rank : ranks) {
		        cards.add(Card.valueOf(suit, rank));
		    }
		}
	}
	
	public Card nextCard() {
	    if(cards.isEmpty()) {
	        throw new IllegalStateException("Deck is empty!");
	    }
	    return cards.remove(0);
	}
	
	public void addCard(Card card) {
	    if(!(cards.size() < SIZE)) {
	        throw new IllegalStateException("Deck is full!");
	    }
	    cards.add(card);
	}
	
	public void addCards(Collection<Card> cards) {	    
	    for(Card card : cards) {
	        addCard(card);
	    }
	}
	
	public void shuffle() {
	    Collections.shuffle(cards);
	}
	
	public int numberOfCards() {
	    return cards.size();
	}
	
	public static void main(String[] args) {
	    Deck d = new Deck();
	    printDeck(d);
	    d.shuffle();
	    printDeck(d);
	    d.shuffle();
	    printDeck(d);
	    d.shuffle();
    }
	
	public static void printDeck(Deck d) {
	    System.out.println("Deck =========================");
	    int lineBreakCount = 1;
	    for(Card c : d.cards) {
	        System.out.print(HoldEmUtils.toStringUnicodeImage(c));	        
	        lineBreakCount++;
	        if(lineBreakCount == 14) {
                System.out.println();
                lineBreakCount = 1;
            }
	    }
	}
}
